package shangguigu_juc.lock;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.concurrent.locks.ReentrantLock;

@Data
@NoArgsConstructor
@AllArgsConstructor
class LTicket {
    private int ticketNum;
    private int saleNum;
    public void sale() {
        if (ticketNum > 0) {
            System.out.println(Thread.currentThread().getName() + ": 卖出：" + (++saleNum) + " 剩下：" + --ticketNum);
        }
    }
}

@Data
@NoArgsConstructor
@AllArgsConstructor
class LTask implements Runnable {
    private LTicket lTicket;
    private final ReentrantLock lock = new ReentrantLock();
    @Override
    public void run() {
        lock.lock();
        try {
            for (int i = 0; i < 10; i++) {
                lTicket.sale();
            }
        } finally {
            lock.unlock();
        }
    }
}

public class MyLSaleTicket {

    public static void main(String[] args) {
        LTicket lTicket = new LTicket(30, 0);
        LTask lTask = new LTask(lTicket);
        new Thread(lTask, "t1").start();
        new Thread(lTask, "t2").start();
        new Thread(lTask, "t3").start();
    }
}
